<?php

class TranslateController extends Zend_Controller_Action 
{
    function init()
    {
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Article');
        Zend_Loader::loadClass('Translation');
        Zend_Loader::loadClass('User');
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }
    
    function indexAction()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        
        
    }
    
    
    function editAction()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'article','edit')) $this->_redirect(DEFAUL_URL);
            
        $this->view->content_title = $this->view->title = "Translate Article";
        $this->view->lang = $this->_request->getParam('lang');
        if(!in_array($this->view->lang,explode(',',LANGUAGES))) {
            echo 'Language ['.$this->view->lang.'] not found. Expected : '.LANGUAGES.'<br>';
            exit();
        }
            
        $obj = new Article();
        $tr = new Translation();
                
        $params = array();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $id = (int)$this->_request->getPost('article');
            
            $params['content'] = trim($this->_request->getPost('content'));
            $params['short_content'] = trim($this->_request->getPost('short_content'));
            $params['subject'] = trim($filter->filter($this->_request->getPost('subject')));
                        
            if ($id !== false) {
                foreach($params  as $field=>$value) {
                    $data = array('itemid'=>$id,
                                  'itemtable'=>'article',
                                  'itemfield'=>$field,
                                  'lang'=>$this->view->lang,
                                  'value'=>$value);
                    if($tid = $tr->getTranslationId($id,'article',$field,$this->view->lang)) {
                        $where = 'id = ' . $tid;
                        $tr->update($data, $where);
                    } else {
                        $tr->insert($data);
                    }
                }
            }
        }
        //else 
        {
            $id = (int)$this->_request->getParam('article', 0);
            
            
            if ($id > 0) {
                $this->view->item = $obj->fetchRow('id='.$id);
                $this->view->tr = $tr->getArticle($id,$this->view->lang);
            }
            if(!$this->view->tr) {
                
                $this->view->tr = new stdClass();
                $this->view->tr->subject = '';
                $this->view->tr->short_content = '';
                $this->view->tr->content = '';
            
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        
        $this->render();
    }
    
    function deleteAction()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'article','delete')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Delete Article";
        
        $obj = new Article();
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();
            
            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));
            
            if ($del == 'Yes' && $id > 0) {
                $data = array(
                        'deleted' => '1'
                );
                $where = 'id = ' . $id;
                $obj->update($data, $where);
            }
        } else {
            $id = (int)$this->_request->getParam('id');
            if ($id > 0) {
                // only render if we have an id and can find the article.
                $this->view->item = $obj->fetchRow('id='.$id);
                
                if ($this->view->item->id > 0) {
                    $this->render();
                    return;
                }
            }
        }
        
        // redirect back to the project list unless we have rendered the view
        $this->_redirect('/article');
    }
    
    public function languageAction() {
        
        $lang = $this->_request->getParam('lang');
        if(in_array($lang,explode(',',LANGUAGES))) {
            $_SESSION['lang'] = $lang;
        } else $_SESSION['lang'] = DEFAUL_LANGUAGE;
        
        $this->_redirect($_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:'/');
    }
    
}